import { SVG } from '@svgdotjs/svg.js'

// 判断泵转
const isRunning = new Map([
  ['tunnling', function (rolateIsOrNot, pumpArrCopy, id) {
    rolateIsOrNot.forEach(el => {
      if (el.key.charAt(1) === id.slice(4) && Number(el.value) > 0) {
        pumpArrCopy.push(document.getElementById(id))
      }
    })
  }],
  ['graphing', function (rolateIsOrNot, pumpArrCopy, id) {
    rolateIsOrNot.forEach(el => {
      // console.log(el, id)
      if (el.key === id.slice(4) && Number(el.value) !== 0) {
        pumpArrCopy.push(document.getElementById(id))
      }
    })
  }]
])
// 线流向的走势
const wayRun = new Map([
  ['tunnelLine', (currentNum) => {
    if (['1', '8', '5', '6'].indexOf(currentNum) >= 0) {
      return true
    } else {
      return false
    }
  }],
  ['calcLine', (currentNum) => {
    if (['1', '6', '7', '8', '9', '10', '11', '12'].indexOf(currentNum) >= 0) {
      return true
    } else {
      return false
    }
  }],
  ['steepLine', (currentNum) => {
    return true
    // if (['1', '8', '5', '6'].indexOf(currentNum) >= 0) {
    //   return true
    // } else {
    //   return false
    // }
  }],
  ['profilingLine', (currentNum) => {
    return true
    // if (['1', '8', '5', '6'].indexOf(currentNum) >= 0) {
    //   return true
    // } else {
    //   return false
    // }
  }], 
  ['feedingLine', (currentNum) => {
    return true
    // if (['1', '8', '5', '6'].indexOf(currentNum) >= 0) {
    //   return true
    // } else {
    //   return false
    // }
  }],
])
// 获取所有线
const getPath = (linePath, pathIdTagName) => {
  const gGroup = document.getElementsByTagName('g')
  for (var i of gGroup) {
    if (i.id.slice(0, 11) === pathIdTagName) {
      linePath.push(document.getElementById(i.id))
    }
  }
}
// 删除title和dec
const delAllTitleAndDec = () => {
  const gGroup = document.getElementsByTagName('g')
  for (var i of gGroup) {
    for (var u of i.childNodes) {
      if (u.nodeName === 'title' || u.nodeName === 'desc') {
        u.parentNode.removeChild(u)
      }
    }
  }
}
// 改值
const updateValue = (textId, value) => {
  const gGroup = document.getElementsByTagName('g')
  for (var i of gGroup) {
    if (i.id === textId) {
      if (i.querySelector('text')) {
        const textItem = SVG(i.querySelector('text'))
        textItem.node.textContent = value
      }
    }
  }
}
// 改虚线
const drawDashLine = (lineitem, lineKinds) => {
  // console.log(lineitem)
  console.log(lineKinds,'这是linekind')
  const pathstyle = lineitem.querySelector('path')
  const pathitem = SVG(pathstyle)
  pathitem.array().forEach(el => {
    el.splice(0, 1)
  })
  // console.log('pathitem.array()', pathitem.array())
  addPoly(lineitem, pathitem.array())
  pathitem.attr({
    'stroke-dasharray': '10 5',
    repeatCount: 'indefinite'
  })
  var stepNum = 15
  if (checkDirection(lineitem.id, lineKinds)) {
    pathitem.attr({ 'stroke-dashoffset': stepNum })
    stepNum = stepNum - 15
  } else {
    pathitem.attr({ 'stroke-dashoffset': 0 })
  }
  pathitem.animate({
    duration: 300,
    when: 'now',
    times: 1
  }).attr({ 'stroke-dashoffset': 0 + stepNum }).loop(true, false).ease('-')
}
// 折线
const addPoly = (lineitem, arrs) => {
  const polylines = SVG(lineitem)
  polylines.polyline(arrs)
    .stroke({
      width: 8,
      color: 'rgba(0,107,140,0.7)'
    }).attr({
      'stroke-linecap': 'round',
      'stroke-linejoin': 'round' // 圆角
    })
}
// 判断流向
const checkDirection = (lineNum, kinds) => {
  // var currentNum = lineNum.charAt(lineNum.length - 1) tmd有两位的
  var currentNum = lineNum.replace(/[^0-9]/ig, '')
  return wayRun.get(kinds)(currentNum)
}
// 初始化泵静止暂停十字
const pumpStatic = (arrs) => {
  var needRotaArr = []
  arrs.forEach(item => {
    var pathItem = item.querySelector('path')
    var xyhw = SVG(pathItem).bbox()
    var groo = SVG(item)
    var linecap1 = groo.line(0, 0, 10, 0).stroke({ width: 1.5, color: 'rgb(1,239,224)' }).move(xyhw.cx - 5, xyhw.cy)
    var linecap2 = groo.line(0, 0, 10, 0).stroke({ width: 1.5, color: 'rgb(1,239,224)' }).move(xyhw.cx - 5, xyhw.cy).rotate(90, xyhw.cx, xyhw.cy)
    var lineTen = [linecap1, linecap2]
    lineTen.forEach(u => {
      const uItem = u.animate({
        duration: 300,
        when: 'now',
        times: true
      }).rotate(180, xyhw.cx, xyhw.cy).ease('-')
      uItem.timeline().pause() // 先默认都处于暂停状态
      needRotaArr.push({
        _id: item.id,
        uItem: uItem
      }) // 2 * 8 十六个线
    })
  })
  return needRotaArr
}
// 选择性转动泵
const pumpRotateChosen = (names, rolateIsOrNot, saveSVGarr) => {
  // console.log(names, rolateIsOrNot, saveSVGarr)
  var pumpArr = []
  const gGroup = document.getElementsByTagName('g')
  for (var i of gGroup) {
    if (i.id.length !== 0 && i.id.slice(0, 4) === 'pump') {
      // map对象 存储每个判断转不转的方法
      isRunning.get(names)(rolateIsOrNot, pumpArr, i.id)
    }
  }
  const pumpArrId = []
  pumpArr.forEach(i => {
    pumpArrId.push(i.id)
  })
  saveSVGarr.forEach(q => {
    if (pumpArrId.indexOf(q._id) !== -1) {
      q.uItem.timeline().play()
    } else {
      q.uItem.timeline().pause()
    }
  })
}

const pumpRotateChosenSteep = (names, saveSVGarr, isProducting) => {
  // console.log('++++', names, saveSVGarr, isProducting)
  const pumpArrId = []
  const gGroup = document.getElementsByTagName('g')
  for (var i of gGroup) {
    if (i.id.length !== 0 && i.id.slice(0, 4) === 'pump') {
      pumpArrId.push(i.id)
    }
  }
  saveSVGarr.forEach(q => {
    if (isProducting) {
      q.uItem.timeline().play()
    } else {
      q.uItem.timeline().pause()
    }
  })
}
// 泵十字
// const pumpRotate = (names, rolateIsOrNot) => {
//   var pumpArr = []
//   const gGroup = document.getElementsByTagName('g')
//   for (var i of gGroup) {
//     if (i.id.length !== 0 && i.id.slice(0, 4) === 'pump') {
//       // 后期写个map对象 存储每个判断转不转的方法
//       isRunning.get(names)(rolateIsOrNot, pumpArr, i.id)
//     }
//   }
//   pumpArr && pumpArr.forEach(item => {
//     var pathBro = item.querySelector('path')
//     var xyhw = SVG(pathBro).bbox()
//     // console.log('item.querySelector()', item.querySelector('line'))
//     // if (item.querySelector('line') !== null && store.getters.getRolateState === true) {
//     //   return
//     // }
//     var gro = SVG(item)
//     var lineIt = gro.line(0, 0, 10, 0).stroke({ width: 1.5, color: 'rgb(1,239,224)' }).move(xyhw.cx - 5, xyhw.cy)
//     var lineIt2 = gro.line(0, 0, 10, 0).stroke({ width: 1.5, color: 'rgb(1,239,224)' }).move(xyhw.cx - 5, xyhw.cy).rotate(90, xyhw.cx, xyhw.cy)
//     var arr = [lineIt, lineIt2]
//     arr.forEach(item => {
//       item.animate({
//         duration: 300,
//         when: 'now',
//         times: 60000
//       }).rotate(180, xyhw.cx, xyhw.cy).ease('-')
//     })
//   })
// }
// 开关闭合
// const changeSwitch = (gid, bool) => {
//   const switchItem = document.getElementById(gid).querySelector('path')
//   let ranges = 0
//   console.log('boolbool', bool)
//   ranges = bool ? ranges = 30 : ranges
//   SVG(switchItem).animate({
//     duration: 500,
//     when: 'now',
//     times: 1
//   }).rotate(ranges, SVG(switchItem).array()[1][1], SVG(switchItem).array()[1][2])
// }

export default {
  getPath,
  drawDashLine,
  updateValue,
  delAllTitleAndDec,
  pumpStatic,
  pumpRotateChosen,
  pumpRotateChosenSteep
}
